package util;

import model.NormalUser;
import model.Record;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

/**
 * @Question RecordDao
 * @Date 2022/12/18 11:57
 * @Solution
 */
public class RecordDao {
    public List<Record> getAll() {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Record> recordList=new LinkedList<>();
        try {
            connection=DBUtil.getConnection();
            String sql = "select * from book_record";
            statement=connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            while(resultSet.next()) {
                Record record = new Record();
                record.setId(resultSet.getInt("id"));
                record.setBookId(resultSet.getInt("book_id"));
                record.setUserId(resultSet.getInt("user_id"));
                record.setOutTime(resultSet.getTimestamp("out_date"));
                record.setInTime(resultSet.getTimestamp("in_date"));
                recordList.add(record);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return recordList;
    }
    public int insert(Record record) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int ret=0;
        try {
            connection=DBUtil.getConnection();
            String sql = "insert into book_record values(null,?,?,?,?)";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,record.getBookId());
            statement.setInt(2,record.getUserId());
            statement.setString(3,record.getStringOutTime());
            statement.setString(4,record.getStringInTime());
            ret = statement.executeUpdate();
            return ret;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return ret;
    }
    public List<Record> getByUserId(int userId) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Record> recordList=new LinkedList<>();
        try {
            connection=DBUtil.getConnection();
            String sql = "select * from book_record where user_id=?";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,userId);
            resultSet = statement.executeQuery();
            while(resultSet.next()) {
                Record record = new Record();
                record.setId(resultSet.getInt("id"));
                record.setBookId(resultSet.getInt("book_id"));
                record.setUserId(resultSet.getInt("user_id"));
                record.setOutTime(resultSet.getTimestamp("out_date"));
                record.setInTime(resultSet.getTimestamp("in_date"));
                recordList.add(record);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return recordList;
    }
    public int reviseInDate(int id,String OutDate) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int ret=0;
        try {
            connection=DBUtil.getConnection();
            String sql = "update book_record set in_date=? where id=?";
            statement=connection.prepareStatement(sql);
            statement.setString(1,OutDate);
            statement.setInt(2,id);
            ret = statement.executeUpdate();
            return ret;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return ret;
    }
    public int deleteById(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        int ret=0;
        try {
            connection=DBUtil.getConnection();
            String sql = "delete from book_record where id=?";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,id);
            ret = statement.executeUpdate();
            return ret;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return ret;
    }
}
